from rj2205.students.student import *


class StudentManger(object):
    def __init__(self):
        self.students = []

    def run(self):
        self.load()
        while True:
            try:
                self.question()
                i = int(input("What function do you want to use"))
                if i == 1:
                    self.add()
                elif i == 2:
                    self.delete()
                elif i == 3:
                    self.upd()
                elif i == 4:
                    self.select_single()
                elif i == 5:
                    self.select_all()
                elif i == 6:
                    self.save()
                else:
                    print("\033[31m Unknown answer, please enter again\033[0m")
                    self.question()
                    self.run()
            except ValueError:
                print("Unknown answer, please enter again")
                self.question()
                self.run()

    def question(self):
        print("1.Add a new student's information\n"
              "2.Delete an existing student's information\n"
              "3.Update an existing student's information\n"
              "4.View single student's information\n"
              "5.View all student's information\n"
              "6.Save student's information into data\n")

    def add(self):
        n_id = input("Please enter new student's ID")
        c = True
        for i in self.students:
            if i.id == n_id:
                c = False
        if c:
            n_name = input("Please enter new student's name")
            n_age = input("Please enter new student's age")
            n_classes = input("Please enter new student's class")
            n_counselor = input("Please enter new student's counselor")
            n_course = input("Please enter new student's course")
            new_student = Student(n_id, n_name, n_age, n_classes, n_counselor, n_course)
            self.students.append(new_student)
        else:
            print("This id is exist,please enter new id")

    def delete(self):
        c = 0
        for i in self.students:
            c += 1
        if c <= 3:
            print("The information is not enough, can't delete more")
        else:
            d_id = input("Please enter ID what you want to delete")
            for i in self.students:
                if i.id == d_id:
                    self.students.remove(i)
                    print("Delete successful")

    def upd(self):
        u_id = input("Please enter ID what you want to update")
        c = 0
        for i in self.students:
            c += 1
            if i.id == u_id:
                c -= 1
                i.name = input("Please enter new name")
                i.age = input("Please enter new age")
                i.classes = input("Please enter new class")
                i.counselor = input("Please enter new counselor")
                i.course = input("Please enter new course")
                self.select_for_s(u_id)
            if c == self.students.__len__():
                print("!!!Not find this student!!!")

    def select_single(self):
        if len(self.students) == 0:
            print("!!!List is null, no data!!!")
        else:
            ssid = input("Please enter student's id what you want to select")
            self.select_for_s(ssid)

    def select_for_s(self, ssid):
        c = 0
        for i in self.students:
            c += 1
            if i.id == ssid:
                c -= 1
                print(i)
            if c == self.students.__len__():
                print("!!!Not find this student!!!")

    def select_all(self):
        if len(self.students) == 0:
            print("!!!List is null, no data here!!!")
        else:
            for i in self.students:
                print(i)

    def save(self):
        f = open("s.data", "w", encoding="utf-8")
        new_list = [i.__dict__ for i in self.students]
        a = str(new_list)
        f.write(a)
        f.close()

    def load(self):
        try:
            f = open("../students/s.data", "r", encoding="utf-8")
        except:
            f = open("../students/s.data", "w", encoding="utf-8")
            a = '[]'
            f.write(a)
        else:
            data = f.read()
            new_list = eval(data)
            self.students = [Student(i["id"], i["name"], i["age"], i["classes"], i["counselor"], i["course"]) for i in
                             new_list]
            f.close()
